<?php
// created by: alfin akhret
// related files :
// 1. mgroup_alias.php
// 2. f-group-alias.php
// 3. grid-group-alias.php
class Group_alias extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('mgroup_alias');
		$this->load->model('mglobal');	     
		$this->load->model('mmaster');
		$this->load->library('form_validation');
	}

	public function index()
	{
	
		$data['template'] = "shell/smooth";
		$data['main_view'] = 'f-group-alias';
		$data['alias_id'] = '';
		$data['alias_name'] = '';
		$data['user_group_id'] = '';


		//untuk kebutuhan update
		if(isset($_GET['update_alias_id']))
		{
			$result = $this->mgroup_alias->get_spesific_group_alias_data($_GET['update_alias_id']);
			foreach ($result as $r) 
			{
				$data['alias_id'] = $r->alias_id;
				$data['alias_name'] = $r->alias_name;
				$data['user_group_id'] = $r->user_group_id;

				$this->session->set_userdata('alias_name_before', $r->alias_name);
				$this->session->set_userdata('user_group_id_before', $r->user_group_id);
			}
		}

		$data['user_group_data'] = $this->mgroup_alias->get_user_group_name();
		$this->load->view($data['template'], $data);
		
	}

	function insert_group_alias()
	{
		$this->form_validation->set_rules('alias_name', 'Alias Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('user_group_id', 'User group name', 'required');

		if ($this->form_validation->run() != FALSE)
		{
			$this->mgroup_alias->insert_group_alias();

			$this->mglobal->saveLog('add group alias', '(added group alias name: '.$this->session->userdata('alias_name').')', $this->session->userdata("username"));

			$this->session->set_flashdata('message', 'Data Berhasil dimasukkan');
			redirect(base_url() . "index.php/group_alias");		
		}

		redirect(base_url() . "index.php/group_alias");

	}

	function get_group_alias_data()
	{
		$result = $this->mgroup_alias->get_group_alias_data();
		echo json_encode($result);
	}

	function update_group_alias()
	{
		$this->form_validation->set_rules('alias_name', 'Alias Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('user_group_id', 'User group name', 'required');

		if ($this->form_validation->run() != FALSE)
		{
			$this->mgroup_alias->update_group_alias($this->input->post('alias_id'), $this->input->post('alias_name'), $this->input->post('user_group_id'));

			$this->mglobal->saveLog('update group alias', '(Updated group alias name from: '.$this->session->userdata("alias_name_before").' to: '.$this->session->userdata("alias_name_after").' | user group id from: '.$this->session->userdata("user_group_id_before").' to: '.$this->session->userdata("user_group_id_after").')', $this->session->userdata("username"));

			$this->session->set_flashdata('message', 'Data Berhasil diperbaharui');
			redirect(base_url() . "index.php/group_alias");		
		}

		redirect(base_url() . "index.php/group_alias");
	}

	function delete($alias_id)
	{
		$result = $this->mgroup_alias->delete($this->uri->segment(3));
		echo "data telah berhasil dihapus";
	}

}